package com.ugoodtech.mdcc.core.domain;

        /* Copyright (C) Ugoodtech, Inc - All Rights Reserved
         * Unauthorized copying of this file, via any medium is strictly prohibited
         */

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * 用户
 */
@ApiModel("用户模型")
@Entity
@Table(name = "users")
public class User extends BaseEntity implements Serializable, UserDetails {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    @JsonIgnore
    private String password;
    @ApiModelProperty("openId")
    private String openId;
    @ApiModelProperty("服务次数")
    private Integer serviceNum = 0;
    @ApiModelProperty("权限")
    private Set<ModuleAuthority> authorities = new HashSet<>();
    @ApiModelProperty("登陆用户类型")
    private UserType userType;
    @ApiModelProperty("维修站")
    private Long stationId;
    @ApiModelProperty("角色")
    private Integer role = 0;
    @ApiModelProperty("用户标识")
    private Integer userFlag = -1;

    @Column(name = "union_id")
    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }

    private String unionId;

    /////////////////////////////////////
    @JsonIgnore
    private boolean accountEnabled;
    @JsonIgnore
    private boolean accountExpired;
    @JsonIgnore
    private boolean accountLocked;
    @JsonIgnore
    private boolean credentialsExpired;

    /**
     * Default constructor - creates a new instance with no values set.
     */
    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Column(nullable = false, length = 50, unique = true)
    public String getUsername() {
        return username;
    }

    @Override
    @Column(nullable = false)
    @JsonIgnore
    public String getPassword() {
        return password;
    }

    @Column(name = "account_enabled")
    public boolean isAccountEnabled() {
        return accountEnabled;
    }

    public void setAccountEnabled(boolean accountEnabled) {
        this.accountEnabled = accountEnabled;
    }


    @Column(name = "account_expired", nullable = false)
    public boolean isAccountExpired() {
        return accountExpired;
    }

    /**
     * @return true if account is still active
     * @see UserDetails#isAccountNonExpired()
     */
    @Override
    @Transient
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return !isAccountExpired();
    }

    @Column(name = "account_locked", nullable = false)
    public boolean isAccountLocked() {
        return accountLocked;
    }

    /**
     * @return false if account is locked
     * @see UserDetails#isAccountNonLocked()
     */
    @Override
    @Transient
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return !isAccountLocked();
    }

    @Column(name = "credentials_expired", nullable = false)
    public boolean isCredentialsExpired() {
        return credentialsExpired;
    }

    /**
     * @return true if credentials haven't expired
     * @see UserDetails#isCredentialsNonExpired()
     */
    @Override
    @Transient
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return !credentialsExpired;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setAccountExpired(boolean accountExpired) {
        this.accountExpired = accountExpired;
    }

    public void setAccountLocked(boolean accountLocked) {
        this.accountLocked = accountLocked;
    }

    public void setCredentialsExpired(boolean credentialsExpired) {
        this.credentialsExpired = credentialsExpired;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return (username != null ? username.hashCode() : 0);
    }

    @Enumerated
    public UserType getUserType() {
        return userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    public Integer getServiceNum() {
        return serviceNum;
    }

    public void setServiceNum(Integer serviceNum) {
        this.serviceNum = serviceNum;
    }

    @Column(name = "role")
    public Integer getRole(){
        return  role;
    }

    public void setRole(Integer role){
        this.role = role;
    }


    @Column(name = "user_flag")
    public Integer getUserFlag(){
        return  userFlag;
    }

    public void setUserFlag(Integer userFlag){
        this.userFlag = userFlag;
    }

    @Column(name = "station_id")
    public Long getStationId() {
        return stationId;
    }

    public void setStationId(Long stationId) {
        this.stationId = stationId;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(FetchMode.SELECT)
    @JoinTable(
            name = "user_authorities",
            joinColumns = {@JoinColumn(name = "user_id")},
            inverseJoinColumns = @JoinColumn(name = "authority_id")
    )
    public Set<ModuleAuthority> getAuthorities() {
        return authorities;
    }

    public void setAuthorities(Set<ModuleAuthority> authorities) {
        this.authorities = authorities;
    }

}
